﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class LinkPanel : BasePanel
{
	public InputField ipInputField;
	public InputField portInputField;

	public int dif { get; set; }
	CanvasGroup canvasGroup;
	private void Awake() {
		canvasGroup = GetComponent<CanvasGroup>();
		uipanelType = UIPanelType.LinkPanel;
		ipInputField.text = "127.0.0.1";
		portInputField.text = "8877";
	}

	public override void OnEnter() {
		canvasGroup.blocksRaycasts = true;
		canvasGroup.alpha = 1;
		transform.SetAsLastSibling();
		GetComponent<RectTransform>().localScale = Vector3.one;
	}

	public override void OnExit() {
		canvasGroup.blocksRaycasts = false;
		canvasGroup.alpha = 0;
		GetComponent<RectTransform>().localScale = Vector3.zero;
	}

	public override void OnPause() {
		canvasGroup.blocksRaycasts = false;
	}

	public override void OnResume() {
		canvasGroup.blocksRaycasts = true;
	}



	public void OnClickClose() {
		uiMng.PopStack();
	}



	public void OnClickEnter() {
		string ip = ipInputField.text;
		int port = 0;
		try {
			port = int.Parse(portInputField.text);
			if (port == 0) {
				Debug.Log("port不能为0");
			}
		} catch {
			Debug.Log("port只能为数字");
		}

		if (port == 0) return;  // 错误
		bool success = true;
		try {
			GameRoot.Instance.clientMng.ConnectServer(ip, port);
		} catch {
			success = false;
		}
		if (success) {
			GetComponent<LinkRequest>().RequestLink(dif);
		}
	}

}
